package com.peycard.persistence.dao;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.peycard.persistence.PersistenceException;
import com.peycard.persistence.dao.local.ReaderListDAOLocal;
import com.peycard.persistence.entities.ReaderControl;
import com.peycard.persistence.entities.ReaderList;

/**
 * Session Bean implementation class ReaderListDAO
 */
@Stateless
public class ReaderListDAO extends GenericDAOImpl<ReaderList, Long> implements
		ReaderListDAOLocal {

	public ReaderListDAO() {
	}

	@Override
	public ReaderList save(String noSerie, String version, String security,
			String encKey, String active, String user, String idCtl)
			throws PersistenceException {
		try {
			ReaderList reader = new ReaderList();
			reader.setEncryptionKey(encKey);
			BigDecimal act = new BigDecimal(active);
			reader.setIsActive(act);
			BigDecimal sec = new BigDecimal(security);
			reader.setMessageSecurity(sec);
			ReaderControl rc = new ReaderControl();
			rc.setIdReaderControl(Long.parseLong(idCtl));
			reader.setReaderControl(rc);
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			reader.setSaveData(ts);
			reader.setSerialNumber(noSerie);
			reader.setUsername(user);
			reader.setVersion(version);
			ReaderList r = super.create(reader);
			return r;
		} catch (Exception e) {
			throw new PersistenceException(e);
		}
	}

	public ReaderList findByNS(String ns) throws PersistenceException{
		try{
			Query query = getEntityManager().createNamedQuery("findReaderListByNS_1");
			query.setParameter("serial_1", ns);
			ReaderList r = (ReaderList) query.getSingleResult();
			return r;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public ReaderList update (String noSerie, String version, String security,
	String encKey, String active, String user, String idCtl)
	throws PersistenceException {
		try{
			ReaderList reader = findByNS(noSerie);
			reader.setEncryptionKey(encKey);
			BigDecimal act = new BigDecimal(active);
			reader.setIsActive(act);
			BigDecimal sec = new BigDecimal(security);
			reader.setMessageSecurity(sec);
			Timestamp ts = new Timestamp(System.currentTimeMillis());
			reader.setSaveData(ts);
			reader.setSerialNumber(noSerie);
			reader.setUsername(user);
			reader.setVersion(version);
			ReaderList r = super.update(reader);
			return r;
		} catch (Exception e) {
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public void setDown(String idReader, String userName) throws PersistenceException{
		try{
			ReaderList reader = super.read(Long.parseLong(idReader));
			reader.setIsActive(BigDecimal.valueOf(0));
			reader.setUsername(userName);
			super.update(reader);
		} catch (Exception e) {
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public List<ReaderList> getAll() throws PersistenceException{
		try{
			Query query = getEntityManager().createNamedQuery("findReaderList_all_1");
			List<ReaderList> list = query.getResultList();
			return list;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}
	
	@Override
	public List<ReaderList> getNV() throws PersistenceException{
		try{
			Query query = getEntityManager().createNamedQuery("findReaderList_NV_1");
			List<ReaderList> list = query.getResultList();
			return list;
		}catch(Exception e){
			throw new PersistenceException(e);
		}
	}

}
